//
//  LineView.swift
//  App_Common_Views
//
//  Created by 蒋晨成 on 2022/2/7.
//

import Adaptor
import UIKit

public class LineView: UIView {
    private let lineColor: UIColor
    private let lineWidth: CGFloat
    public init(frame: CGRect, lineColor: UIColor, lineWidth: CGFloat) {
        self.lineColor = lineColor
        self.lineWidth = lineWidth / Adaptor.Screen.scale
        super.init(frame: frame)
        backgroundColor = UIColor.clear
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func draw(_ rect: CGRect) {
        super.draw(rect)

        guard let context = UIGraphicsGetCurrentContext(), !lineColor.isEqual(UIColor.clear), lineWidth > 0 else {
            return
        }

        let path = CGMutablePath()
        let y = rect.maxY - lineWidth
        path.move(to: CGPoint(x: 0, y: y))
        path.addLine(to: CGPoint(x: rect.size.width, y: y))
        context.addPath(path)
        context.setStrokeColor(lineColor.cgColor)
        context.setLineWidth(lineWidth)
        context.strokePath()
    }
}
